home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MYIO.ZIP / MYIODEMO.CPP < prev    next >
C/C++ Source or Header  |  1995-11-02  |  4KB  |  169 lines

  1. // Myiodemo.cpp
  2. // This is a trivial program which uses the myio loopback class
  3. // to demonstrate the basics on writing an io interface using
  4. // the AT&T C++ iostream classes.
  5. // The program simply provides the ability to selectively add
  6. // to or read from a Myio instance and display information to
  7. // assist in understanding how it all works.
  8. //
  9.  
  10. # include "Mystream.h"      // This includes Myio.h and iostream.h
  11. # include "myLine.h"
  12. # include <conio.h>         // For getch()
  13. # include <ctype.h>         // For toupper()
  14. # include <string.h>
  15.  
  16. # define NL char('\n')
  17.  
  18.     // Let's do the "application is a class" trick
  19.  
  20. class myApplication
  21. {
  22.             // Defines a pointer to member function type
  23.             // used for dispatching the menu
  24.  
  25.     typedef void (myApplication::*pvf) (void);
  26.  
  27.   public:
  28.  
  29.     myApplication (void) : mio() {}
  30.     int execute (void);
  31.  
  32.   private:
  33.  
  34.     iostream & stream (void) { return mio.stream(); }
  35.     int domenu (void);
  36.     void send (void);
  37.     void read (void);
  38.     void disp (void);
  39.     void peek (void);
  40.     void flsh (void);
  41.     void stat (void);
  42.  
  43.     pvf choice;     // Function called to execute
  44.     Myio mio;       // IO object
  45.  
  46. };
  47.  
  48.  
  49. void
  50. myApplication::disp (void)
  51. {
  52.     cout << "Mystream status:" << NL
  53.          << "Chrs in output buffer = " << stream().rdbuf()->out_waiting() << NL
  54.  
  55.          << "Chrs in  input buffer = " << stream().rdbuf()->in_avail()    << NL
  56.  
  57.          << "Myio object status = "
  58.             << mio.count() << char('/') << mio.size()
  59.             << " LastWrite=" << (mio.writeok() ? "OK" : "Incomplete")
  60.             << " LastRead=" << (mio.readok() ? "OK" : "EOF")
  61.          << endl;
  62. }
  63.  
  64.     // Request a line and send it to the IO device
  65.  
  66. void
  67. myApplication::send (void)
  68. {
  69.     cout << NL << "Enter text to write - press <ENTER> when done\n:";
  70.     myLine L;
  71.     cin >> L;
  72.     int l = strlen(L);
  73.     if (!l)
  74.         cerr << "Nothing entered." << endl;
  75.     else
  76.     {
  77.         cout << "Writing '"
  78.              << L
  79.              << char('\'')
  80.              << endl;
  81.         stream() << L << NL;    // Send the entered data, NL terminated
  82.         cout << "Chrs written to Myio object = " << (l + 1) << NL;
  83.         disp ();
  84.     }
  85. }
  86.  
  87. void
  88. myApplication::read (void)
  89. {
  90.     cout << NL << "Reading a line from object:" << NL;
  91.     myLine L;
  92.     mio.stream().clear();
  93.     mio.stream() >> L;
  94.     int l = strlen(L);
  95.     if (!l)
  96.     {
  97.         cout << "Nothing read." << endl;
  98.         mio.stream().clear();       // Clear EOF status
  99.     }
  100.     else
  101.     {
  102.         cout << "Read '"
  103.              << L
  104.              << char('\'')
  105.              << endl;
  106.         cout << "Chrs read from Myio object = " << (l + 1) << NL;
  107.         disp ();
  108.     }
  109. }
  110.  
  111. void
  112. myApplication::flsh (void)
  113. {
  114.     cout << NL << "Flushing stream" << endl;
  115.     stream() << flush;
  116.     disp ();
  117. }
  118.  
  119. void
  120. myApplication::stat (void)
  121. {
  122.     cout << NL << "Myio object buffer dump:" << NL;
  123.     mio.dump();
  124.     disp ();
  125.     stream().rdbuf()->dbp();    // Dump stream info
  126. }
  127.  
  128. int
  129. myApplication::domenu (void)
  130. {
  131.     cout << NL
  132.          << "W)rite  R)ead  D)ump  F)lush  Q)uit\n"
  133.          << "Select: "
  134.          << flush;      // Need to flush here for portability
  135.     int key;
  136.     for (;;)
  137.     {
  138.         key = getch ();
  139.         switch (toupper(key))
  140.         {
  141.         case 'W': choice = &myApplication::send;    break;
  142.         case 'R': choice = &myApplication::read;    break;
  143.         case 'D': choice = &myApplication::stat;    break;
  144.         case 'F': choice = &myApplication::flsh;    break;
  145.         case 'Q': key = 0;                          break;
  146.         default:
  147.             continue;
  148.         }
  149.         cout << char(key) << endl;
  150.         break;
  151.     }
  152.     return key;
  153. }
  154.  
  155. int                         // This is really the application
  156. myApplication::execute (void)
  157. {
  158.     while (domenu ())
  159.         (this->*choice) ();
  160.     return 0;
  161. }
  162.  
  163. int
  164. main (void)
  165. {
  166.     myApplication Demo;     // Declare the application
  167.     return Demo.execute (); // go for it!
  168. }
  169.